<template>
  <el-dialog class='subject-status-add-record' title="发布版本" width="600px" :show-close='true'
             :visible.sync="publishVisible" :before-close="handleClose">
    <div>
      <el-form :rules="rules" ref="formRef" size="mini" :model="formData" label-width="140px">
        <el-form-item label="发布环境：" prop="fromType">
          <el-radio-group v-model="formData.fromType" @change="handleEnvironmentChange">
            <el-radio label="1">DEV</el-radio>
            <el-radio label="2">PRO</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="eCRF版本号：" v-if="formData.fromType === '2'" prop="keyForECrf">
          <el-select v-model="formData.keyForECrf" placeholder="请选择" style="width: 80%">
            <el-option v-for="item in eCRFDataList" :key="item.id" :label="item.versionNum" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button :loading='loading' size="small" type="primary" @click="handleSave">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { mapActions } from 'vuex'

export default {
  props: {
    publishVisible: Boolean,
    projectId: String,
    publishData: Object
  },

  data() {
    return {
      rules: {
        fromType: [{ required: true, message: '请选择', trigger: 'blur' }],
        keyForECrf: [{ required: true, message: '请选择', trigger: 'blur' }]
      },
      loading: false,
      eCRFDataList: [],
      formData: {}
    }
  },

  methods: {
    ...mapActions('solutionVersion', ['publishDatabaseVersion', 'qryECRFDataList']),

    // 关闭取消
    handleClose() {
      this.$emit('handlePublishCancel')
    },

    getECRFDataList() {
      const params = {
        projectId: this.projectId,
      }
      this.qryECRFDataList({ params }).then((res) => {
        if (res.code === 200 && res.data) {
          res.data.map((rItem, rIndex) => { 
            if (rItem.isUsed === 0) { // 未使用的eCRF版本
              this.eCRFDataList.push(rItem)
            }
          })
        }
      })
    },

    // 环境改变事件
    handleEnvironmentChange(value) {
      if (value === '2') {
        this.getECRFDataList()
      }

      // 重置ecrf数据
      if (value === '1') {
        this.formData.keyForECrf = ''
        this.eCRFDataList = []
      }
    },

    // 保存提交
    handleSave() {
      this.loading = true
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          const data = {
            ...this.formData,
            projectId: this.projectId,
            databaseVersion: this.publishData.id,
            type: this.formData.fromType === '1' ? '2' : this.formData.fromType === '2' ? '3' : ''
          }
          this.publishDatabaseVersion({ data }).then(res => {
            const { code } = res
            if (code === 200) {
              this.$message({
                message: '保存成功',
                type: 'success'
              })
              this.$refs.formRef.resetFields()
              this.$emit('handlePublishCancel', 'refresh')
            }
            this.loading = false
          })
        } else {
          this.loading = false
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import './publish.scss';
</style>
